#!/usr/bin/python3
#encoding:utf-8
import os, shutil, datetime


def stamp(filePath):
    t = os.path.getmtime(filePath)
    return t


def xcopy(path, out):
    for files in os.listdir(path):
        src = os.path.join(path, files)
        dst = os.path.join(out, files)

        if os.path.isfile(src):
            if os.path.exists(dst):
                if stamp(src) > stamp(dst):
                    shutil.copy(src, dst)
                    print(src)
            else:
                if not os.path.exists(os.path.dirname(dst)):
                    os.makedirs(os.path.dirname(dst))
                shutil.copy(src, dst)
                print(src)
        else:
            if not os.path.isdir(dst):
                os.makedirs(dst)
            xcopy(src, dst)


ifix = os.path.abspath(os.path.dirname(__file__))
projDir = ifix

index = len(ifix)
if "Documents" in ifix:
    index = ifix.find("Documents")
InjectFix_Home = ifix[0:index] + "Documents/WorkSpace_demo/InjectFix"
if not os.path.exists(InjectFix_Home):
    InjectFix_Home = input("请输入InjectFix目录：")
if "Packages" in ifix:
    projDir = ifix[0:ifix.find("Packages")]
elif "Library" in ifix:
    projDir = ifix[0:ifix.find("Library")]
elif "Assets" in ifix:
    projDir = ifix[0:ifix.find("Assets")]

if projDir == ifix:
    projDir = input("指定当前Unity项目：")
# 编译injectfix项目和工具
CoreProj = os.path.join(InjectFix_Home, "Source/VSProj/vs2013/IFix.Core.csproj")
os.system("msbuild " + CoreProj)
ToolProj = os.path.join(InjectFix_Home, "Source/VSProj/vs2013/IFix.csproj")
os.system("msbuild " + ToolProj)

# IFix.exe
path = os.path.join(InjectFix_Home, "Source/VSProj/Bin")
xcopy(path, os.path.join(projDir, "IFixToolKit"))

# IFix脚本
path = os.path.join(InjectFix_Home, "Source/UnityProj/Assets/IFix")
xcopy(path, ifix)
# IFix.Core.dll
path = os.path.join(InjectFix_Home, "Source/VSProj/Lib")
xcopy(path, os.path.join(ifix, "Plugins"))
# 项目 Plugins
# xcopy(path, os.path.join(projDir, "Assets/Plugins"))

# 开发时热更插件
path = os.path.join(InjectFix_Home, "Source/Misc/LiveDotNet")
xcopy(path, ifix)

str = input("exit with any key:")